//  AppLed.c
//
//  Created on: 2019年7月16日
//  Author: cludu
//

#include "../inc/AppLed.h"


tLEDMode_Def tLedProcess[LED_MAX];

GpioConfig_Def LedConfig[LED_MAX] =
{
    // LED0 的配置
    {
        .GpioPort    = GpioPort1,
        .GpioPin     = GpioPin0,
        .GpioMode    = GpioMode_OUT,    //
        .ActiveLogic = Bit_SET,
        .OutputLogic = Bit_RESET,          // 上电默认LED OFF
    },
    // LED1 的配置
    {
        .GpioPort    = GpioPort1,
        .GpioPin     = GpioPin6,
        .GpioMode    = GpioMode_OUT,    //
        .ActiveLogic = Bit_SET,
        .OutputLogic = Bit_RESET,          // 上电默认LED OFF
    },

};


/*****************************************************************************
名称: tLED_Init
说明: LED模块的任务，在LED事件被置位的时候，将被执行。
输入: NONE
返回: NONE
说明:
******************************************************************************/
void tLED_Init(void)
{
    uint8_t tIndex;
    for(tIndex = 0; tIndex < LED_MAX; tIndex++)
    {
            Gpio_Init(&LedConfig[tIndex]);
    }

    memset((uint8_t*)&tLedProcess, 0, sizeof(tLedProcess));

    for(tIndex = 0; tIndex < LED_MAX; tIndex++)
    {
        tLedProcess[tIndex].tLedMode  = LED_OFF;
        tLedProcess[tIndex].tLedState = LED_OFF;
    }
    tLED_Config(LED0, LED_FLASH, 0, 400, 4);
}


/*****************************************************************************
名称: tLED_Task
说明: LED模块的任务，在LED事件被置位的时候，将被执行。
输入: NONE
返回: NONE
说明:
******************************************************************************/

void tLED_Task(void)
{
    uint8_t tLedState;
    uint8_t tIndex;
    for(tIndex = 0; tIndex < LED_MAX; tIndex++)
    {
        tLedState = LED_OFF;  // 默认关闭
        if(tLedProcess[tIndex].tLedMode == LED_FLASH)
        {
            if(tLedProcess[tIndex].tLedDelayCnt >= LedSFlashTime)
            {
                tLedProcess[tIndex].tLedDelayCnt -= LedSFlashTime;
            }
            else
            {
                tLedProcess[tIndex].tLedDelayCnt = 0;
            }

            if(tLedProcess[tIndex].tLedDelayCnt > 0)
            {
                continue;
            }

            tLedState = ((tLedProcess[tIndex].tLedState) ? (LED_OFF) : (LED_ON) == LED_ON);
            tLedProcess[tIndex].tLedDelayCnt =  tLedProcess[tIndex].tLedPeriod;

            //如果tLedTime = ~0 则一直闪烁下去
            if(255 == tLedProcess[tIndex].tLedTime)
            {
                continue;
            }
            if(--tLedProcess[tIndex].tLedTime == 0)
            {
                tLedProcess[tIndex].tLedMode = LED_OFF;
            }
        }
        else
        {
            // 刷新计数
            if(tLedProcess[tIndex].tLedTimeCnt >= LedSFlashTime)
            {
                tLedProcess[tIndex].tLedTimeCnt -= LedSFlashTime;
            }
            else
            {
                tLedProcess[tIndex].tLedTimeCnt = 0;
            }

            if((tLedProcess[tIndex].tLedTimeCnt > 0) || (tLedProcess[tIndex].tLedTimeOut == ~0))
            {
                continue;
            }
            tLedState = LED_OFF;    //时间到了就关闭
        }

        if(tLedState == tLedProcess[tIndex].tLedState)
        {
            continue;
        }
        tLED_Operate(tIndex, tLedState);
        tLedProcess[tIndex].tLedState = tLedState;
    }
}




/*****************************************************************************
名称: tLED_Task
说明: LED模块的任务，在LED事件被置位的时候，将被执行。
输入: NONE
返回: NONE
说明:
******************************************************************************/
void tLED_Operate(uint8_t LedId, uint8_t LedOpMode)
{
    uint8_t GpioPort;
    uint8_t GpioPin;
    BitAction PinState;

    if(LedId >= LED_MAX)
    {
        return;
    }

    GpioPort = LedConfig[LedId].GpioPort;
    GpioPin  = LedConfig[LedId].GpioPin;
    PinState = LedConfig[LedId].OutputLogic;
    switch(LedOpMode)
    {
        case LED_ON:
        {
            Gpio_WritePin(GpioPort, GpioPin, (BitAction)(1 - PinState));

        }break;

        case LED_OFF:
        {
            Gpio_WritePin(GpioPort, GpioPin, PinState);

        }break;

        case LED_FLASH:
        {
            Gpio_TogglePin(GpioPort, GpioPin);

        }break;

        default:
            break;
    }
}


void tLED_Config(uint8_t LedId, uint8_t OpMode, uint32_t ulTimeOut, uint16_t usPeriod, uint8_t ucTime)
{
    uint8_t HWOpMode;
    // 参数check
    if(LedId >= LED_MAX)
    {
        return;
    }
    HWOpMode = LED_OFF;
    memset((uint8_t*)&tLedProcess[LedId], 0, sizeof(tLedProcess));

    tLedProcess[LedId].tLedMode   = OpMode;

    if(OpMode == LED_FLASH)
    {
        tLedProcess[LedId].tLedPeriod     = usPeriod;
        tLedProcess[LedId].tLedDelayCnt   = usPeriod;
        tLedProcess[LedId].tLedTime       = ucTime * 2 - 1;
        HWOpMode = LED_FLASH;
        tLedProcess[LedId].tLedState = LED_ON;
    }
    else
    {
        tLedProcess[LedId].tLedTimeOut = ulTimeOut;
        tLedProcess[LedId].tLedTimeCnt = ulTimeOut;
        if(OpMode == LED_ON)
        {
            HWOpMode = LED_ON;
            tLedProcess[LedId].tLedState = LED_ON;
        }
    }
    tLED_Operate(LedId, HWOpMode);
}



void tLED_TimeProc(uint32_t Period)
{
    static uint32_t LedTick = 0;
    LedTick += Period;
    if(LedTick >= LedSFlashTime)
    {
        Set_Task(6, 6);
        LedTick -= LedSFlashTime;
    }
}

//////////////////////////////////end of file//////////////////////////////////

